package org.agilelang.fgl.parser;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Util {

    static public String error = "";

    static public String getTypeObject(String input) {
        StringBuilder builder = new StringBuilder(input.length());
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '.') {
                break;
            }
            builder.append(c);
        }
        return builder.toString();
    }

    static public String getTypeObject2(String input) {
        int num = 0;
        StringBuilder builder = new StringBuilder(input.length());
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '.') {
                num++;
                continue;
            }
            if (num > 0 && (c == '.' || c == '(')) {
                break;
            }
            if (num == 1) {
                builder.append(c);
            }
        }
        return builder.toString();
    }

    static public String[] split(String str, String sig) {
        String[] paramdel = str.split(sig);
        int l = paramdel.length;
        for (int i = 0; i < l; i++) {
            paramdel[i] = paramdel[i].trim();
        }
        return paramdel;
    }
    static public String[] split_comas(String str) {

        List<String> list = new ArrayList<String>();
        int l = str.length();
        String aux = "";
        boolean flag = false;
        for (int i = 0; i < l; i++) {
            if (str.charAt(i) == ',' && flag == false) {
                //System.out.println(aux.trim());
                list.add(aux.trim());
                aux = "";
            } else {
                aux += str.charAt(i);
            }
            if (str.charAt(i) == '"' && flag == false) {
                
                flag = true;
                continue;
            }
            if (str.charAt(i) == '"' && flag == true) {
                
                flag = false;
            }
        }
        //System.out.println(aux.trim());
        list.add(aux.trim());
        int t = list.size();
        String[] paramdel = new String[t];
        for (int i = 0; i < t; i++) {
            paramdel[i] = list.get(i);
        }
        return paramdel;
    }

    static public boolean setField(Class cls, String name, Object obj, String val) {
        try {
            //System.err.println(name);
            Field field = cls.getField(name);
            field.set(obj, val);
            return true;
        } catch (IllegalArgumentException ex) {
            System.err.println(ex.toString());
            error = ex.toString();
        } catch (IllegalAccessException ex) {
            System.err.println(ex.toString());
            error = ex.toString();
        } catch (NoSuchFieldException ex) {
            System.err.println(ex.toString());
            error = ex.toString();
        } catch (SecurityException ex) {
            System.err.println(ex.toString());
            error = ex.toString();
        }
        return false;
    }

    static public void setMethod(Object attr, Class imagen_class, Object imagen, String fun) {
        Class partypes[] = new Class[1];
        partypes[0] = attr.getClass();
        Method meth = null;
        try {
            meth = imagen_class.getMethod(fun, partypes);
            Object arglist[] = new Object[1];
            arglist[0] = attr;
            meth.invoke(imagen, arglist);
        } catch (IllegalAccessException ex) {
            System.err.println("Error -->" + ex.toString());
        } catch (IllegalArgumentException ex) {
            System.err.println("Error -->" + ex.toString());
        } catch (InvocationTargetException ex) {
            System.err.println("Error -->" + ex.toString());
        } catch (NoSuchMethodException ex) {
            System.err.println("Error -->" + ex.toString());
        } catch (SecurityException ex) {
            System.err.println("Error -->" + ex.toString());
        }

    }

    static public List splitcomas(String doc) {
        boolean value = false;
        List<String> list = new ArrayList<String>();
        StringBuilder builder = new StringBuilder(doc.length());
        for (int i = 0; i < doc.length(); i++) {
            char c = doc.charAt(i);
            if (c == '"') {
                if (value) {
                    value = false;
                } else {
                    value = true;
                }
            }
            if (c == ',' && value == false) {
                System.out.println(" -->> " + builder.toString());

                builder = new StringBuilder(doc.length());

                continue;

            }
            builder.append(c);
        }
        return list;
    }

    static public Function getFunctionApp(String aLine) {

        int num = 0;
        StringBuilder builder = new StringBuilder(aLine.length());
        for (int i = 0; i < aLine.length(); i++) {
            char c = aLine.charAt(i);
            if (c == '.' && num == 0) {
                num++;
                continue;
            }
            if (c == ')') {
                builder.append(c);
                break;
            }

            if (num == 1) {
                builder.append(c);
            }
        }

        Function fun = new Function(builder.toString());
        return fun;
    }

    static public Function getFunction(String aLine) {
        int num = 0;
        StringBuilder builder = new StringBuilder(aLine.length());
        for (int i = 0; i < aLine.length(); i++) {
            char c = aLine.charAt(i);
            if (c == '.' && (num == 0 || num == 1)) {
                num++;
                continue;
            }
            if (c == ')') {
                builder.append(c);
                break;
            }

            if (num == 2) {
                builder.append(c);
            }
        }
        Function fun = new Function(builder.toString());
        return fun;
    }

    static public String getSecondtoken(String input) {
        int num = 0;
        StringBuilder builder = new StringBuilder(input.length());
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '.') {
                num++;
                continue;
            }

            if (num == 2) {
                break;
            }
            if (num == 1) {
                builder.append(c);
            }
        }
        return builder.toString().trim();
    }

    static public String replaceSpecial(String input) {
        StringBuilder builder = new StringBuilder(input.length());

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            switch (c) {
                case '.':
                    builder.append("/.");
                    break;
                //case '%': builder.append("percentage"); break;
                //case '@': builder.append("at"); break;
                default:
                    builder.append(c);
                    break;
            }
        }
        return builder.toString();
    }
}
